package com.gitee.wish;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.AuthClient;
import com.gitee.wish.dto.OrdersDTO;
import com.gitee.wish.vo.WishOrder;
import com.gitee.wish.vo.WishToken;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by EalenXie on 2022/2/23 9:51
 */
public class WishClient implements AuthClient {


    private final ObjectMapper mapper;


    private final RestOperations restOperations;
    /**
     * Wish Host 地址 https://merchant.wish.co
     */
    private static final String HOST = "https://merchant.wish.com";


    public WishClient() {
        this(new RestTemplate());
    }

    public WishClient(RestOperations restOperations) {
        this(new ObjectMapper(), restOperations);
    }

    public WishClient(ObjectMapper objectMapper, RestOperations restOperations) {
        this.mapper = objectMapper;
        this.restOperations = restOperations;
    }

    /**
     * 获取授权地址   文档: https://china-merchant.wish.com/documentation/api/v3/reference#section/Authentication
     *
     * @param state 应用程序生成的状态值。
     */
    public String authorize(String clientId, String state) {
        return String.format("%s/v3/oauth/authorize?client_id=%s&state=%s", HOST, clientId, state);
    }


    /**
     * 授权码模式获取accessToken  文档: https://china-merchant.wish.com/documentation/api/v3/reference#operation/oauthAccessToken
     *
     * @param clientId     客户端Id
     * @param clientSecret 客户端密钥
     * @param code         授权码
     * @param redirectUri  重定向地址
     */
    public ResponseEntity<WishToken> accessToken(String clientId, String clientSecret, String code, String redirectUri) {
        HttpHeaders headers = getBasicHeaders(clientId, clientSecret);
        return restOperations.exchange(URI.create(String.format("%s/api/v3/oauth/access_token?grant_type=authorization_code&code=%s&redirect_uri=%s", HOST, code, redirectUri)), HttpMethod.POST, new HttpEntity<>(null, headers), WishToken.class);
    }


    /**
     * 获取订单详情
     * 接口文档  https://china-merchant.wish.com/documentation/api/v3/reference#operation/GetOrder
     *
     * @param orderId     订单Id
     * @param accessToken 令牌
     * @return {@link WishOrder} 订单详情
     */
    public ResponseEntity<WishOrder> getOrder(String orderId, String accessToken) {
        HttpHeaders headers = getBearerHeaders(accessToken);
        return restOperations.exchange(URI.create(String.format("%s/api/v3/orders/%s", HOST, orderId)), HttpMethod.GET, new HttpEntity<>(null, headers), WishOrder.class);
    }

    /**
     * 获取订单列表
     * 接口文档  https://china-merchant.wish.com/documentation/api/v3/reference#operation/GetMultipleOrders
     *
     * @param dto         订单请求参数
     * @param accessToken 令牌
     * @return {@link WishOrder} 订单详情
     */
    public ResponseEntity<List<WishOrder>> getOrders(OrdersDTO dto, String accessToken) {
        HttpHeaders headers = getBearerHeaders(accessToken);
        @SuppressWarnings("unchecked") Map<String, ?> args = mapper.convertValue(dto, Map.class);
        return restOperations.exchange(String.format("%s/api/v3/orders", HOST), HttpMethod.GET, new HttpEntity<>(null, headers), new ParameterizedTypeReference<List<WishOrder>>() {
        }, args != null ? args : new LinkedHashMap<>());
    }


}
